package com.lovo.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.system.dto.PageDto;
import com.lovo.system.dto.count.SaleMonthDto;
import com.lovo.system.dto.count.SaleTypeDto;
import com.lovo.system.service.ISaleStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/saleStatistics")
public class SaleStatisticsController {

    @Autowired
    ISaleStatisticsService saleStatisticsService;

    @RequestMapping("/selectType")
    public List<SaleTypeDto> selectType(@RequestBody PageDto<SaleTypeDto> page) {
        return saleStatisticsService.selectType(page.getStartTime(), page.getEndTime());
    }

    @RequestMapping("/selectBrand")
    public List<SaleTypeDto> selectBrand(@RequestBody PageDto<SaleTypeDto> page) {
        return saleStatisticsService.selectBrand(page.getStartTime(), page.getEndTime());
    }

    @RequestMapping("/selectMonth")
    public List<SaleMonthDto> selectMonth(String year) {
        List<SaleMonthDto> listMonth = new ArrayList<>();
        List<SaleTypeDto> list = saleStatisticsService.selectMonth(year);
        String[] months = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        for (int i = 0; i < months.length; i++) {
            SaleMonthDto saleMonthDto = new SaleMonthDto();
            saleMonthDto.setMonths(months[i]);
            listMonth.add(saleMonthDto);
        }
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < listMonth.size(); j++) {
                if (list.get(i).getMonth().equals(listMonth.get(j).getMonths())){
                    if (list.get(i).getType() == 1){
                        listMonth.get(j).setComputerProducts(list.get(i).getSumPrice());
                    }
                    if (list.get(i).getType() == 2){
                        listMonth.get(j).setSecurityProducts(list.get(i).getSumPrice());
                    }
                    if (list.get(i).getType() == 3){
                        listMonth.get(j).setPrintScanningEquipment(list.get(i).getSumPrice());
                    }
                    if (list.get(i).getType() == 4){
                        listMonth.get(j).setOfficeConsumables(list.get(i).getSumPrice());
                    }
                }
            }
        }
        return listMonth;
    }

}
